package com.yunlin.blecontroler;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.util.Log;
import com.yunlin.utils.FormatUtils;
import java.util.List;
import java.util.UUID;

/*
    ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
       Author   :  lixiaodaoaaa
       Date     :  2018/7/9
       Time     :  16:07
    ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 */
public class BleCharasticServices{
    
    
    private static String TAG=BleCharasticServices.class.getSimpleName();
    
    //服务和特征值
    
    
    private UUID write_UUID_service=UUID.fromString("0000ffe7-1212-efde-1523-785feabcd123");
    private UUID write_UUID_chara=UUID.fromString("0000fec7-1212-efde-1523-785feabcd123");
    
    
    private UUID read_UUID_service=UUID.fromString("0000ffe7-1212-efde-1523-785feabcd123");
    private UUID read_UUID_chara=UUID.fromString("0000fec8-1212-efde-1523-785feabcd123");
    private UUID notify_UUID_service=UUID.fromString("0000ffe7-1212-efde-1523-785feabcd123");
    private UUID notify_UUID_chara=UUID.fromString("0000fec8-1212-efde-1523-785feabcd123");
    
    
    private BluetoothGatt mBluetoothGatt;
    private BleCharaInitListner bleCharaInitListner;
    
    
    public BleCharasticServices(BluetoothGatt mBluetoothGatt, BleCharaInitListner bleCharaInitListner){
        this.mBluetoothGatt=mBluetoothGatt;
        this.bleCharaInitListner=bleCharaInitListner;
    }
    
    public void initServiceAndChara(){
        if (mBluetoothGatt == null) {
            Log.i(TAG, " mBluetoothGatt is null cannot initServiceAndChara");
            destory();
            return;
        }
        List<BluetoothGattService> bluetoothGattServices=mBluetoothGatt.getServices();
        int serviceSize=bluetoothGattServices.size();
        enableNofiManu();
        for (BluetoothGattService bluetoothGattService : bluetoothGattServices) {
            List<BluetoothGattCharacteristic> characteristics=bluetoothGattService.getCharacteristics();
            for (BluetoothGattCharacteristic characteristic : characteristics) {
                enableNotify(characteristic);
                int charaProp=characteristic.getProperties();
                if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                    notify_UUID_chara=characteristic.getUuid();
                    notify_UUID_service=bluetoothGattService.getUuid();
                    mBluetoothGatt.setCharacteristicNotification(characteristic, true);
                    Log.e(TAG, "notify_chara=" + notify_UUID_chara + "----notify_service=" + notify_UUID_service);
                }
                
            }
        }
        
        if (serviceSize < 1) {
            bleCharaInitListner.onInitError();
        }else {
            bleCharaInitListner.onInitSuccess();
        }
    }
    
    private void enableNofiManu(){
        BluetoothGattService notifyService=mBluetoothGatt.getService(notify_UUID_service);
        if (notifyService == null) {
            Log.i(TAG, " notifyService is null ");
            return;
        }
        BluetoothGattCharacteristic notifyChar=notifyService.getCharacteristic(notify_UUID_chara);
        mBluetoothGatt.setCharacteristicNotification(notifyChar, true);
    }
    
    
    public void destory(){
        try {
            if (mBluetoothGatt != null) {
                mBluetoothGatt.disconnect();
                if (mBluetoothGatt != null) {
                    mBluetoothGatt.close();
                }
                mBluetoothGatt=null;
            }
            Log.i(TAG, " is destory  the  BleController  ");
        } catch (Exception e) {
            Log.i(TAG, "destory the ble error  msg is " + e.getMessage());
        }
    }
    
    public void readData(){
        if (mBluetoothGatt == null) {
            Log.i(TAG, " mBluetoothGatt is null  readData cannot execute ");
            return;
        }
        BluetoothGattService readService=mBluetoothGatt.getService(read_UUID_service);
        if (readService == null) {
            Log.i(TAG, " readService is null cannote readData ");
            return;
        }
        BluetoothGattCharacteristic readChara=readService.getCharacteristic(read_UUID_chara);
        mBluetoothGatt.readCharacteristic(readChara);
    }
    
    
    public void writeData(String hexData){
        
        if (mBluetoothGatt == null) {
            Log.i(TAG, " mBluetoothGatt is nulll write data error");
            return;
        }
        
        
        BluetoothGattService service=mBluetoothGatt.getService(write_UUID_service);
        if (service == null) {
            Log.i(TAG, " blueTooth write service is null  cannot  write data to ble");
            bleCharaInitListner.onInitError();
            return;
        }
        BluetoothGattCharacteristic charaWrite=service.getCharacteristic(write_UUID_chara);
        
        
        if (charaWrite == null) {
            Log.i(TAG, "charaWrite is null cannot write data ");
            return;
        }
        Log.i(TAG, "  write data  is " + hexData);
        byte[] data=FormatUtils.strCommandToBytes(hexData);
        charaWrite.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
        charaWrite.setValue(data);
        if (mBluetoothGatt == null) {
            
            Log.i(TAG, "mBluetoothGatt  is null  cannot write");
            return;
        }
        boolean writeResult=mBluetoothGatt.writeCharacteristic(charaWrite);
    }
    
    
    private void enableNotify(BluetoothGattCharacteristic characteristic){
        List<BluetoothGattDescriptor> descriptors=characteristic.getDescriptors();
        for (BluetoothGattDescriptor dp : descriptors) {
            if (dp != null) {
                dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                if (mBluetoothGatt != null) {
                    mBluetoothGatt.writeDescriptor(dp);
                }
            }
        }
    }
}
